package com.lizhi.menu.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lizhi
 * @desc
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //开启路由鉴权
        registry.addInterceptor(new SaInterceptor(handler -> {
            //登录拦截 鉴权
            SaRouter.match("/**")
                    .notMatch(notLogin())
                    .check(StpUtil::checkLogin);
        })).addPathPatterns("/**");
    }

    /**
     * 不拦截路径
     *
     * @Description: notLogin
     * @return: List<String>
     * @Author: bxy
     * @updateBy: bxy
     * @Date: 2022/10/18 15:27
     */
    public static List<String> notLogin() {
        List<String> allows = new ArrayList<>();
        allows.add("/system/user/login");
        allows.add("/system/user/register");
        //swagger资源放行
        allows.add("/v2/**");
        allows.add("/v3/**");
        allows.add("/swagger-ui.html/**");
        allows.add("/swagger-ui/**");
        allows.add("/doc.html/**");
        allows.add("/doc.html");

        allows.add("/api-docs-ext/**");
        allows.add("/swagger-resources/**");
        allows.add("/webjars/**");
        allows.add("/favicon.ico");
        allows.add("/error");
        allows.add("/app/accounts");
        return allows;
    }
}
